import os

import yaml


# 读取项目下配置文件的工具
def read_yaml(yaml_path_relative):
    try:
        # 打开文件
        yaml_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), yaml_path_relative)
        with open(yaml_path, "r", encoding="utf-8") as f:
            data = yaml.load(f, Loader=yaml.FullLoader)
            return data
    except:
        return None


# 解析用户的的图数据信息
# 返回的连线数据类型是三元表
def data_resolve(node_file, link_file):
    node_path_abs = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), node_file)
    link_path_abs = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), link_file)
    nodes = []
    links = []
    with open(node_path_abs, "r", encoding="utf-8") as f:
        data_list = f.readlines()
        for el in data_list:
            nodes.append(el.strip('\n'))
    with open(link_path_abs, "r", encoding="utf-8") as f:
        data_list = f.readlines()
        for el in data_list:
            el_list = el.strip('\n').split(' ')
            el_list[2] = int(el_list[2])
            links.append(el_list)

    return nodes, links

# 起点终点文件解析
def st_resolve(st_file):
    st_path_abs = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), st_file)
    with open(st_path_abs, "r", encoding="utf-8") as f:
        data_list = f.readline().split(' ')
        source = data_list[0]
        target = data_list[1]
    return source, target


if __name__ == '__main__':
    print(data_resolve('data/node.txt', 'data/link.txt'))
